package redis

import (
	"context"
	"fmt"
	"srv/cart"
	"strconv"
)

func AddCart(cart map[string]interface{}) error {
	key := fmt.Sprintf("cart:%d:%d", cart["user_id"], cart["good_id"])
	return Rdb.HMSet(context.Background(), key, cart).Err()

}
func CartExist(userId, goodId int64) bool {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	return Rdb.Exists(context.Background(), key).Val() == 1
}
func UpdateCart(userId, goodId, num int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	return Rdb.HSet(context.Background(), key, "num", num).Err()
}
func DelCart(userId, goodId int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	return Rdb.Del(context.Background(), key).Err()
}
func ClearCart(userId int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		Rdb.Del(context.Background(), v)
	}
}
func StateCart(userId int64, state int64) (err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		Rdb.HSet(context.Background(), v, "good_state", state)
	}
	return err
}
func CountCart(userId int64) (count int64, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {

		carts := Rdb.HGetAll(context.Background(), v).Val()
		if carts["good_state"] == "1" {
			num, _ := strconv.Atoi(carts["num"])
			count += int64(num)
		}

	}
	return count, err
}
func AmountCart(userId int64) (amount float64, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		carts := Rdb.HGetAll(context.Background(), v).Val()
		if carts["good_state"] == "1" {
			num, _ := strconv.Atoi(carts["num"])
			price, _ := strconv.ParseFloat(carts["good_price"], 64)
			amount += float64(num) * price
		}

	}
	return amount, err
}
func ListCart(userId int64) (List []*cart.CartList, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		carts := Rdb.HGetAll(context.Background(), v).Val()
		UserId, _ := strconv.Atoi(carts["user_id"])
		GoodId, _ := strconv.Atoi(carts["good_id"])
		GoodPrice, _ := strconv.ParseFloat(carts["good_price"], 64)
		GoodState, _ := strconv.Atoi(carts["good_state"])
		GoodTitle := carts["good_title"]

		List = append(List, &cart.CartList{
			UserId:    int64(UserId),
			GoodId:    int64(GoodId),
			GoodTitle: GoodTitle,
			GoodPrice: uint64(GoodPrice),
			GoodState: int64(GoodState),
		})

	}
	return
}
func List1Cart(userId, goodId int64) (List []*cart.CartList, err error) {
	key := fmt.Sprintf("cart:%d:%d", userId, goodId)
	carts := Rdb.HGetAll(context.Background(), key).Val()
	UserId, _ := strconv.Atoi(carts["user_id"])
	GoodId, _ := strconv.Atoi(carts["good_id"])
	GoodPrice, _ := strconv.ParseFloat(carts["good_price"], 64)
	GoodState, _ := strconv.Atoi(carts["good_state"])
	GoodTitle := carts["good_title"]

	List = append(List, &cart.CartList{
		UserId:    int64(UserId),
		GoodId:    int64(GoodId),
		GoodTitle: GoodTitle,
		GoodPrice: uint64(GoodPrice),
		GoodState: int64(GoodState),
	})
	return
}
